<?php
error_reporting(0);
if(isset($_POST['cName']) && isset($_POST['cPhone']) && isset($_POST['cEmail'])){	
	if ($_GET['f'] && !empty($_GET['f'])) {
	    $filename = $_GET['f'];
	    saveInfo();
	    download($filename);
	}
	//die("Access denied!");
}else {
	die("Access denied!");
}

function download($filename){
	$type = strtolower(substr(strrchr($filename, "."), 1));
    switch ($type) {
        case "pdf": $ctype = "application/pdf";
            break;
        case "exe": $ctype = "application/octet-stream";
            break;
        case "zip": $ctype = "application/zip";
            break;
        case "doc": $ctype = "application/msword";
            break;
        case "xls": $ctype = "application/vnd.ms-excel";
            break;
        case "ppt": $ctype = "application/vnd.ms-powerpoint";
            break;
        case "gif": $ctype = "image/gif";
            break;
        case "png": $ctype = "image/png";
            break;
        case "jpeg":
        case "jpg": $ctype = "image/jpeg";
            break;
        case "mp3": $ctype = "audio/mpeg";
            break;
        case "wav": $ctype = "audio/x-wav";
            break;
        case "mpeg":
        case "mpg":
        case "mpe": $ctype = "video/mpeg";
            break;
        case "mov": $ctype = "video/quicktime";
            break;
        case "avi": $ctype = "video/x-msvideo";
            break;
        //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
        case "php":
        case "htm":
        case "html":
        case "txt": die("<b>Cannot be used for " . $type . " files!</b>");
            break;
        default: $ctype = "application/force-download";
            break;
    }
    ob_clean();  							
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Type: $ctype");
    header("Content-Disposition: attachment; filename=" . $filename);
    header("Content-Transfer-Encoding: binary");
    header('Content-Length: ' . @filesize('download/' . $filename));
    set_time_limit(0);
    readfile('download/' . $filename);
    exit();
}

function saveInfo(){
	include 'Db.php';
	$Conn = new Connection("localhost", "solvaym_solvay", "solvaym_solvay", "hjKL67*(");
        $Conn->close();
        //get download info
        $name = trim($_POST['cName']);
        $Phone = trim($_POST['cPhone']);
        $email = trim($_POST['cEmail']);
        $address = trim($_POST['cAddress']);
        //check info in db
        $result = $Conn->mysql_fetch_row("SELECT * FROM members WHERE member_name LIKE '".$name."' AND member_phone LIKE '".$Phone."' AND member_email LIKE '".$email."'");
        if(empty ($result)){
           $result = $Conn->query("INSERT INTO members (member_name, member_phone, member_email, member_address, member_createddate) VALUES ('$name', '$Phone', '$email', '$address', '".date('Y-m-d H:i:s')."')"); 
        }
} 

?>